import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:like_button/like_button.dart';
import 'package:sainbainu/common/values/colors.dart';

typedef CommentTap = void Function();
typedef ShareTap = void Function();
typedef LikeTap = void Function(bool flag);

const iconSize = 20.0;
const numSize = 14.0;

class IconNumRow extends StatelessWidget {
  final int giveFlag; // 0.未点赞；1.已点赞
  final int giveNum;
  final int collectionFlag; // 0.未收藏；1.已收藏
  final int collectionNum;
  final int visitNum;
  final int commentNum;
  final int shareNum;
  final Widget? rightChild;
  final MainAxisAlignment mainAxisAlignment;
  final CommentTap? onCommentTap;
  final LikeTap? onLikeTap;
  final LikeTap? onCollectionTap;
  final ShareTap? onShareTap;

  const IconNumRow({
    Key? key,
    this.giveFlag = 0,
    this.giveNum = 0,
    this.collectionFlag = 0,
    this.collectionNum = 0,
    this.visitNum = 0,
    this.commentNum = 0,
    this.shareNum = 0,
    this.rightChild,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.onCommentTap,
    this.onLikeTap,
    this.onCollectionTap,
    this.onShareTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.max,
      children: [
        Expanded(
          child: Row(
            mainAxisAlignment: mainAxisAlignment,
            children: [
              LikeButton(
                size: iconSize,
                isLiked: giveFlag == 1,
                likeCount: giveNum,
                likeBuilder: (bool isLiked) {
                  String lovePath = isLiked
                      ? 'assets/images/icon-num-love@.png'
                      : 'assets/images/icon-num-love.png';
                  return Image.asset(
                    lovePath,
                    width: iconSize,
                    height: iconSize,
                  );
                },
                countBuilder: (int? count, bool isLiked, String text) {
                  return Text(
                    text,
                    style: TextStyle(
                      fontSize: numSize,
                      color: AppColors.unactive4,
                      fontWeight: isLiked ? FontWeight.w600 : FontWeight.normal,
                    ),
                  );
                },
                onTap: (isLiked) async {
                  if (onLikeTap != null) {
                    HapticFeedback.mediumImpact();
                    onLikeTap!(!isLiked);
                  }
                  return !isLiked;
                },
              ),
            ],
          ),
        ),
        Expanded(
          child: Row(
            mainAxisAlignment: mainAxisAlignment,
            children: [
              LikeButton(
                size: iconSize - 2,
                isLiked: collectionFlag == 1,
                likeCount: collectionNum,
                likeBuilder: (bool isLiked) {
                  String lovePath = isLiked
                      ? 'assets/images/icon-num-star@.png'
                      : 'assets/images/icon-num-star.png';
                  return Image.asset(
                    lovePath,
                    width: iconSize - 2,
                    height: iconSize - 2,
                  );
                },
                countBuilder: (int? count, bool isLiked, String text) {
                  return Text(
                    text,
                    style: TextStyle(
                      fontSize: numSize,
                      color: AppColors.unactive4,
                      fontWeight: isLiked ? FontWeight.w600 : FontWeight.normal,
                    ),
                  );
                },
                onTap: (isLiked) async {
                  if (onCollectionTap != null) {
                    HapticFeedback.mediumImpact();
                    onCollectionTap!(!isLiked);
                  }
                  return !isLiked;
                },
              ),
            ],
          ),
        ),
        // Expanded(
        //   child: _buildIconNumItem(
        //     icon: 'assets/images/icon-num-eye.png',
        //     count: visitNum,
        //   ),
        // ),
        Expanded(
          child: _buildIconNumItem(
            icon: 'assets/images/icon-num-comment.png',
            count: commentNum,
            onTap: onCommentTap,
          ),
        ),
        Expanded(
          child: _buildIconNumItem(
            icon: 'assets/images/icon-num-share.png',
            count: shareNum,
            onTap: onShareTap,
          ),
        ),
        if (rightChild != null) rightChild!,
      ],
    );
  }

  Widget _buildIconNumItem({
    required String icon,
    required int count,
    void Function()? onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 12),
        width: double.infinity,
        color: Colors.transparent,
        child: Row(
          mainAxisAlignment: mainAxisAlignment,
          mainAxisSize: MainAxisSize.max,
          children: [
            Image.asset(
              icon,
              width: iconSize,
              height: iconSize,
            ),
            const SizedBox(width: 4),
            Text(
              '$count',
              style: const TextStyle(
                fontSize: numSize,
                color: AppColors.unactive3,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
